  // pages/myshop/index.js
var app = getApp()
var c = require("../common.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
  flaginfo: 0,    
  curit:1, //tab是直播还是 视频
  video:[],
  zhibo:[],
  catehide:1,
  onename:"",
  twocate:[],
  threecate:[],
  curname: "",//当前分类名称
  curid: "",//当前分类id 要和cutit区分 别弄混了
  curtwoname:"",//二级分类名称
  curtwoid:"",//二级分类id
  curthreename:"",//三级分类名称
  curthreeid:"",//三级分类id
  hot: 0,//人气
  price: 0,//价钱
  fontheight:null,//点亮人气
  fontheightprice:null,//点亮价钱
  page: 1 //点亮价钱
  },
  //更改直播和视频显示
  changeit(e){
 
  this.setData({
    curit:e.currentTarget.dataset.it,
    fontheight: null,
    fontheightprice:null,
    hot: 0,
    price: 0,
    page: 1, //点亮价钱
    catehide:1
  })
  },
  //菜单显示隐藏
  changecatehide(){
    this.setData({
      catehide:this.data.catehide?0:1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    //接收标签值
  
    var flaginfo=0;
    this.setData({ fileurl: app.testimgurl})
    if (e.flaginfo=="putong"){
     this.setData({
       flaginfo:0
     })
    }
    else if (e.flaginfo=="mianfei"){
      this.setData({
        flaginfo: 1
      })
    }
    else if (e.flaginfo=="tuijian"){
      this.setData({
        flaginfo: 2
      })
    }
    //加载一级菜单下包括自身在内的所有菜单
    console.log(1222);
    console.log(e.cate_id);
    this.loadallcate(e.cate_id)
    
  },
 //加载一级菜单下包括自身在内的所有菜单
  loadallcate(id){
    var that=this
    var flaginfo = this.data.flaginfo;
    console.log(flaginfo);
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/loadallcate",
      { id: id },
      res => {
        this.setData({
          allcate: res.data,
          onename:res.data.name,
          twocate:res.data.child,
          threecate:res.data.child[0].child,
          curname:res.data.name,
          curid:res.data.cate_id,
          curtwoname:res.data.child[0].name,
          curtwoid: res.data.child[0].cate_id
        })
  
        this.loadvideo(res.data.cate_id)
        this.loadzhibo(res.data.cate_id)
      }
    )
  },
  //通过菜单id获取视频
  loadvideo: function (id, hot, price, page, flaginfo){
    var flaginfo = this.data.flaginfo;
  c.ajax(
    app.globalData.rootPath+"/index.php/index/Wxapi/loadvideobycateid",
    { id: id, hot: hot, price: price, page: page, flaginfo: flaginfo},
    res=>{
     this.setData({
       video:res.data
     })
    }
  )
  },
  //通过菜单id获取直播
  loadzhibo(id, hot, price, page) {
    var flaginfo = this.data.flaginfo;
    c.ajax(
      app.globalData.rootPath + "/index.php/index/Wxapi/loadzhibobycateid",
      { id: id, hot: hot, price: price, page: page, flaginfo: flaginfo},
      res => {
        this.setData({
          zhibo: res.data
        })
      }
    )
  },
  //价钱
  priceUp: function () {
    var price = this.data.price;
    this.setData({ catehide: 1 });
    if (price == 1) {
      this.setData({ price: 2 });
    } else if (price == 2){
      this.setData({ price: 1 });
    } else if (price == 0){
      this.setData({ price: 1 });
    }
    
    this.setData({ fontheightprice: 1 });
    this.setData({ fontheight: 0 });
    
    var cid = this.data.curid;
    var price = this.data.price;
    var hot = 0;

    this.setData({ page: 1 });

    this.loadvideo(cid, hot, price);
    this.loadzhibo(cid, hot, price);
  },
  //人气优先
  peopleUp: function () {
    var curit = this.data.curit;
    var hot = this.data.hot;
    this.setData({ price: 0 });
    this.setData({ catehide:1});
    if (hot == 1) {
      this.setData({ hot: 2 });
    } else if (hot == 2) {
      this.setData({ hot: 1 });
    } else if (hot == 0){
      this.setData({ hot: 1 });
    }
    this.setData({ fontheight: 1 });
    this.setData({ fontheightprice: 0 });

    var cid = this.data.curid;
    var hot = this.data.hot;
    var price = 0;
    
    this.setData({ page: 1 });
    if (curit==1){ //视频
      this.loadvideo(cid, hot, price);
    }else{//直播
      this.loadzhibo(cid, hot, price);
    }
    


  },
  loadvideomore: function (id, price, hot, page){
    var that=this;
    wx.request({
      url: app.globalData.rootPath + "/index.php/index/Wxapi/loadvideobycateid",
      data: { id: id, hot: hot, price: price, page: page },
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        var beforvideo = that.data.video;
        var aftervideo = res.data;
        var all = beforvideo.concat(res.data);

        that.setData({
          video: all
        }) 
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  zhibovideomore: function (id, price, hot, page) {
    var that = this;
    wx.request({
      url: app.globalData.rootPath + "/index.php/index/Wxapi/loadzhibobycateid",
      data: { id: id, hot: hot, price: price, page: page },
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        var beforvideo = that.data.video;
        var aftervideo = res.data;
        var all = beforvideo.concat(res.data);

        that.setData({
          video: all
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //上拉到最低端 触发
  moreinfo: function () {
    var id   = this.data.curid;
    var curit = this.data.curit;
    var price=this.data.price;
    var hot  = this.data.hot;
    var page = this.data.page;
    this.setData({page:page+1});

    if (curit==1){
      this.loadvideomore(id, price, hot, page);
    }else{
      this.zhibovideomore(id, price, hot, page);
    }
    
  },

  //改变当前的菜单id和name
  changecur(e){
   this.setData({
     curname:e.currentTarget.dataset.name,
     curid:e.currentTarget.dataset.cate_id,
     catehide:1,
     curtwoname:"",
     curtwoid:"",
     curthreeid:"",
     curthreename:""
   })
   this.loadvideo(e.currentTarget.dataset.cate_id)
   this.loadzhibo(e.currentTarget.dataset.cate_id)
  },
  //改变当前菜单的name和id为二级对应
  changecurtwo(e){
    var all=this.data.allcate.child
    var res=[]
    for(var i=0;i<all.length;i++){
      if (all[i]["cate_id"] == e.currentTarget.dataset.cate_id){
        res=all[i]["child"]
      }
    }
    this.setData({
      catehide: 1,
      curname: e.currentTarget.dataset.name,
      curid: e.currentTarget.dataset.cate_id,
      curtwoname: e.currentTarget.dataset.name,
      curtwoid: e.currentTarget.dataset.cate_id,
      threecate:res,
      curthreeid: "",
      curthreename: ""
    })
    this.loadvideo(e.currentTarget.dataset.cate_id)
    this.loadzhibo(e.currentTarget.dataset.cate_id)
  },
  //改变当前菜单name和id为三级菜单
  changecurthree(e) {
    this.setData({
      curname: e.currentTarget.dataset.name,
      curid: e.currentTarget.dataset.cate_id,
      catehide: 1,
      curthreeid: e.currentTarget.dataset.cate_id,
      curthreename: e.currentTarget.dataset.name
    })
    this.loadvideo(e.currentTarget.dataset.cate_id)
    this.loadzhibo(e.currentTarget.dataset.cate_id)
  },
  //全部按钮专用
  selectcurtwo() {
    this.setData({
      curname: this.data.curtwoname,
      curid: this.data.curid,
      catehide: 1,
      curthreeid: "",
      curthreename: ""
    })
    this.loadvideo(this.data.curid)
    this.loadzhibo(this.data.curid)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var id = this.data.curid;
    var hot = this.data.hot;
    var price = this.data.price;
    var curit = this.data.curit;
    var page = 0; 
    if (curit==1){
      this.loadvideo(id, hot, price, page);  
    }else{
      this.loadzhibo(id, hot, price, page); 
    }
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  inDetail: function (e) {
    // 商品类型：

    // 1 -> 一对一直播
    // 2 -> 单节视频
    // 3 -> 套课
    // 4 -> 套课中的视频
    // 5 -> 预约直播
    // 6 -> 一对多直播
    var gid = e.currentTarget.dataset.goodsid;
    var sql = 'w(goods_id=' + gid + ')f(type)l(1)';
    c.ajax(
      app.dbs + 'goods',
      { sql: sql },
      res => {
        var typeinfo = res.data.type;
        console.log(typeinfo);
        if (typeinfo == 2) {
          wx.navigateTo({
            url: '/pages/detailsdanke/detailsdanke?goods_id=' + gid,
          })
        } else if (typeinfo == 3) {
          wx.navigateTo({
            url: '/pages/detailtaoke/detailtaoke?goods_id=' + gid,
          })
        } else if (typeinfo == 1 || typeinfo == 6) {
          wx.navigateTo({
            url: '/pages/detailzhibo/detailzhibo?goods_id=' + gid,
          })
        }
      }
    )
  }
})